﻿package component.fileupload.util;

import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

import component.fileupload.exception.FileUploadException;

public class DataViewUtil {
	public static byte[] writeObject(Object object) throws FileUploadException {
		try {
			ByteArrayOutputStream baos = new ByteArrayOutputStream();
			ObjectOutputStream oos = new ObjectOutputStream(baos);
			oos.writeObject(object);
			byte[] ab = baos.toByteArray();
			oos.close();
			baos.close();
			return ab;
		} catch (IOException e) {
			e.printStackTrace();
			throw new FileUploadException("convert object to byte array error："
					+ e.getMessage());
		}
	}

	public static Object readObject(InputStream inputStream)
			throws FileUploadException {
		try {
			ObjectInputStream ois = new ObjectInputStream(inputStream);
			Object o = ois.readObject();

			ois.close();
			return o;
		} catch (IOException e) {
			e.printStackTrace();
			throw new FileUploadException("convert stream to object error: "
					+ e.getMessage());
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
			throw new FileUploadException(e);
		}
	}

	public static URLConnection getCon(String urlcon)
			throws FileUploadException {
		java.net.URL url = null;
		try {
			url = new java.net.URL(urlcon);
		} catch (MalformedURLException e) {
			e.printStackTrace();
			throw new FileUploadException(e);
		}
		// java.net.URLConnection con = null;
		HttpURLConnection con = null;
		try {
			con = (HttpURLConnection) url.openConnection();
		} catch (IOException e) {
			e.printStackTrace();
			throw new FileUploadException(e);
		}
		//con.setRequestMethod("POST");
		con.setUseCaches(true);
		con.setDoOutput(true);
		con.setDoInput(true);
		// con.setRequestProperty("Content-Type", "multipart/form-data;");// 设置表单类型
		con.setRequestProperty("Content-type", "application/octest-stream");
		// con.setRequestProperty("Content-Length", ); //设置内容长度

		return con;

	}

	public Object parseObject(String httpUrl, Object obj1)
			throws FileUploadException {
		try {
			Object obj2 = null;
			URLConnection con = getCon(httpUrl);
			// 写入数据
			if (obj1 != null) {
				DataOutputStream dataout = new DataOutputStream(con
						.getOutputStream());
				byte[] buf = writeObject(obj1);
				dataout.write(buf);
				dataout.flush();
				dataout.close();
			}

			// 读取数据
			DataInputStream in = new DataInputStream(con.getInputStream());
			obj2 = readObject(in);
			in.close();

			return obj2;
		} catch (IOException e) {
			e.printStackTrace();
			throw new FileUploadException(e);
		}
	}
}
